<%-- 
    Document   : gen_report_on_AllOrders
    Created on : May 3, 2013, 7:36:12 PM
    Author     : Ivchenko Oleg
--%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.AbstractList"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.math.BigDecimal"%>

<%@page import="javax.faces.convert.BigDecimalConverter"%>

<%@page import="entity.Orders"%>
<%@page import="entity.Warehouse"%>
<%@page import="entity.Users"%>
<%@page import="facade.OrdersFacadeLocal"%>
<%@page import="facade.WarehouseFacadeLocal"%>
<%@page import="facade.UsersFacadeLocal" %>
<%@page import="java.util.List"%>
<%@page import="javax.naming.NamingException"%>
<%@page import="javax.naming.InitialContext"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%! private WarehouseFacadeLocal warehouseFacade;
    private UsersFacadeLocal usersFacade;
    private OrdersFacadeLocal ordersFacade;

    public void jspInit() {
        try {
            InitialContext ic = new InitialContext();
            ordersFacade = (OrdersFacadeLocal) ic.lookup("java:global/Delta/Delta-ejb/OrdersFacade!facade.OrdersFacadeLocal");
            warehouseFacade = (WarehouseFacadeLocal) ic.lookup("java:global/Delta/Delta-ejb/WarehouseFacade!facade.WarehouseFacadeLocal");
            usersFacade = (UsersFacadeLocal) ic.lookup("java:global/Delta/Delta-ejb/UsersFacade!facade.UsersFacadeLocal");
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
    }
%>

<script> 
    function selectElement(){
        document.getElementById("selectForm").submit();
    }
</script>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="css/styl.css">
        <link rel="stylesheet" type="text/css" href="css/gen_reports.css">
        <title>Generation report on shipment orders</title>
    </head>
    <body>
        <header>
            <div id ="top_name"> <h1> Warehouse employee</h1> </div>
            <div id="back">
                <p> <a href="main.jsp"><< Back to shop </a> </p> 
            </div>
            <div id="registr2">
                <p> <a href="user_name.jsp">User name</a>
                    <a href="main.jsp">LogOut</a> </p>
            </div>
            <div id="menu">
                <div id="line"> <a href="WarehouseOrders"> Orders</a> </div>
                <div id="line"> <a href="warehouse.jsp"> Warehouses</a> </div>
                <div id="line"> <a href="catalog_product.jsp"> Product</a> </div>
            </div>
        </header>
        <div id="main">
            <div>
                <form method="GET" id="selectForm" style="position: relative; left: 4%; "action="gen_report_on_AllOrders.jsp">
                    <p>
                        Status
                        <select
                            name="status"
                            size=1
                            onchange="selectElement()">
                            <option value="">
                                all
                            </option>
                            <%
                                String strStatus = request.getParameter("status");
                                if (strStatus == null && strStatus == "") {
                                    strStatus = "all";
                                }
                            %>
                            <%for (String status : ordersFacade.getListStatuses()) {%>
                            <option 
                                value=<%=status%>
                                <%=status == strStatus ? "selected" : ""%>>
                                <%= status%>
                            </option>
                            <%}%>
                        </select> 

                        Warehouse
                        <select  
                            name="warehouse" 
                            size = 1
                            onchange="selectElement()">
                            <option 
                                value=""> all
                            </option>
                            <%
                                String strId_warehouse = request.getParameter("warehouse");
                                int idWarehouse;
                                if (strId_warehouse != null && strId_warehouse != "") {
                                    idWarehouse = Integer.parseInt(strId_warehouse);
                                } else {
                                    idWarehouse = 0;//-1
                                }
                            %>
                            <%for (Warehouse w : warehouseFacade.findAll()) {%>
                            <option 
                                value=<%=w.getIdWarehouse()%>
                                <%=w.getIdWarehouse() == idWarehouse ? "selected" : ""%>>
                                <%= w.getName()%>
                            </option>
                            <%}%>
                        </select> 
                    </p>
                    <br>
                    <br>
                    <div id="sel_user"> <p> User
                            <select  
                                name="user" 
                                size = 1
                                onchange="selectElement()">
                                <option 
                                    value=""> all
                                </option>
                                <%String strId_user = request.getParameter("user");
                                    int idUser;
                                    if (strId_user != null && strId_user != "") {
                                        idUser = Integer.parseInt(strId_user);
                                    } else {
                                        idUser = 0;
                                    }
                                %>
                                <%for (Users u : usersFacade.findAll()) {%>
                                <option 
                                    value=<%=u.getIdUser()%>
                                    <%=u.getIdUser() == idUser ? "selected" : ""%>>
                                    <%= u.getLastName()%> 
                                </option>
                                <%}%>
                            </select> 
                        </p>
                    </div>
                           
                </form>
            </div>
            <div id="tabl">
                <form id ="tabl">
                    <p>Orders:</p>
                    <table width="90%">
                        <TR>
                            <td id="head_table_first" >Order #</td>
                            <td id="head_table_first">Order Date </td>
                            <td id="head_table_first">  Order Status</td>
                            <td id="head_table_first">Warehouse </td>
                            <td id="head_table_first">User </td>
                            <td id="head_table_first">Address </td>
                            <td id="head_table_first">  Shipping price</td>
                            <td id="head_table_first">  Product price</td>
                        </TR>

                        <%
                            //List<Orders> ordersList = (List) session.getAttribute("ordersListR");
                            List<Orders> ordersList = ordersFacade.findByStatusUniversal(strStatus, idUser, idWarehouse);
                            if (ordersList == null) {
                        %>
                        <tr>
                            <td id ="colomn_first" colspan="8">
                                No orders found
                            </td>
                        </tr>
                        <%                        }
                            for (Orders o : ordersList) {
                        %>
                        <TR>
                            <td id="colomn_first" > <%=o.getTrackingNumber()%></td>   
                            <td id="colomn_first" > <%=o.getOrderDate()%></td>  
                            <td id="colomn_first" > <%=o.getOrderStatus()%></td> 
                            <td id="colomn_first" > <%=o.getIdWarehouse().getName()%></td> 
                            <%
                                if (o.getIdUser() != null) {
                            %>
                            <td id="colomn_first" ><%=o.getIdUser().getFirstName() + " " + o.getIdUser().getLastName()%></td>
                            <%
                            } else {
                            %>
                            <td id="colomn_first" > <%="NULL"%></td>
                            <%}
                            %>
                            <td id="colomn_first" > <%=o.getAddress()%></td>
                            <td id="colomn_first" > <%=o.getShippingPrice()%></td>
                            <td id="colomn_first" > <%=o.getProductPrice()%></td>
                        </TR>
                        <%}%>
                    </table>
                </form>
            </div>
            <%int pageNum = (Integer) request.getAttribute("countR");
                int currentPage;
                if (request.getParameter("page") != null) {
                    if (Integer.parseInt(request.getParameter("page")) < 1) {
                        currentPage = 1;
                    } else if (Integer.parseInt(request.getParameter("page")) > pageNum) {
                        currentPage = pageNum;
                    } else {
                        currentPage = Integer.parseInt(request.getParameter("page"));
                    }
                } else {
                    currentPage = 1;
                }%>
            <div id="paging">
                <div id="line_button"> 
                    <button 
                        id="navigation" 
                        name="back" 
                        onclick="window.location.href = '<%="WarehouseOrders?page=" + (currentPage - 1)%>'" <%= currentPage > 1 ? "" : "disabled"%>> 
                        << 
                    </button><currentPage%></div>

                <div id="line_button"> 
                    <button 
                        id="navigation" 
                        name="forward" 
                        onclick="window.location.href = '<%="WarehouseOrders?page=" + (currentPage + 1)%>'"<%= currentPage < pageNum ? "" : "disabled"%>> 
                        >> 
                    </button>
                </div>
                <form method="GET" action="catalog_product.jsp"> 
                    <div id="back"> 
                        <button>
                            Back
                        </button> 
                    </div>
                </form>
                <div id="expExcel"> 
                    <form method="POST" action="ReportBuildingAllSO">   
                        <button 
                            type="submit" 
                            name="export" 
                            onclick="selectElement()"
                            >
                            Export to Excel
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </body>
</html>